package com.mybatisplus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybatisplus.mapper.UserMapper;
import com.mybatisplus.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserMapperTester {
    @Resource
    private UserMapper userMapper;

    @Test
    public void testSelectById(){//主键查询
        User user=userMapper.selectById(2L);
        System.out.println("usrName"+user.getUsrName());
    }
    @Test
    public void testInsert(){//添加
        userMapper.insert(new User("MyBatis-Plus","123456",2L,1));
    }
    @Test
    public void testFindAll(){//查询所有
        List<User> list=userMapper.selectList(null);
        for (User user : list) {
            System.out.println("usrName:"+user.getUsrName());
        }
    }
    @Test
    public void testFind(){//条件查询：根据用户名和密码查询
        QueryWrapper<User> wrapper=new QueryWrapper<User>();
        wrapper.eq("usr_name","MyBatis-Plus");
        wrapper.eq("usr_password","123456");
        List<User> list=userMapper.selectList(wrapper);
        if(list!=null){
            for (User user : list) {
                System.out.println("usrId:"+user.getUsrId());
            }
        }

    }
    @Test
    public void testPagination(){//分页查询
        QueryWrapper<User> wrapper=new QueryWrapper<User>();
        wrapper.eq("usr_role_id",2L);//查询条件：角色Id为2
        //分页对象：查询获得User对象，查看第1页数据，每页显示2条记录
        Page<User> page=new Page<User>();
        IPage<User> userIPage=userMapper.selectPage(page,wrapper);
        System.out.println("总记录数--------->"+userIPage.getTotal());
        System.out.println("总页数--------->"+userIPage.getPages());
        System.out.println("当前页数--------->"+userIPage.getCurrent());
        System.out.println("每页记录数--------->"+userIPage.getSize());
        System.out.println("当前记录:--->");
        for (User user : userIPage.getRecords()) {//获得查询记录
            System.out.println("usrId:"+user.getUsrId());

        }

    }

}
